#include "reader.h"
#include "ui_reader.h"
#include "Variate.h"

Reader::Reader(QWidget *parent) :
    QMyPageWidget(parent),
    ui(new Ui::Reader)
{
    ui->setupUi(this);

    db = DBOperation::getInstance();
    db->open();

    getData();
    ui->lineEdit_Address->setEnabled(false);
    ui->lineEdit_Birthday->setEnabled(false);
    ui->lineEdit_Email->setEnabled(false);
    ui->lineEdit_Sex->setEnabled(false);
    ui->lineEdit_TEL->setEnabled(false);
    ui->LineEdit_Name->setEnabled(false);

    ui->LineEdit_Name->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                     "color: rgb(31,31,31);"		//字体颜色
                                     "padding-left:20px;"       //内边距-字体缩进
                                     "background-color: rgb(255, 255, 255);" //背景颜色
                                     "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->lineEdit_Address->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                        "color: rgb(31,31,31);"		//字体颜色
                                        "padding-left:20px;"       //内边距-字体缩进
                                        "background-color: rgb(255, 255, 255);" //背景颜色
                                        "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->lineEdit_Birthday->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                         "color: rgb(31,31,31);"		//字体颜色
                                         "padding-left:20px;"       //内边距-字体缩进
                                         "background-color: rgb(255, 255, 255);" //背景颜色
                                         "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->lineEdit_Email->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                      "color: rgb(31,31,31);"		//字体颜色
                                      "padding-left:20px;"       //内边距-字体缩进
                                      "background-color: rgb(255, 255, 255);" //背景颜色
                                      "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->lineEdit_Sex->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                    "color: rgb(31,31,31);"		//字体颜色
                                    "padding-left:20px;"       //内边距-字体缩进
                                    "background-color: rgb(255, 255, 255);" //背景颜色
                                    "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->lineEdit_TEL->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                    "color: rgb(31,31,31);"		//字体颜色
                                    "padding-left:20px;"       //内边距-字体缩进
                                    "background-color: rgb(255, 255, 255);" //背景颜色
                                    "border:2px solid rgb(20,196,188);border-radius:15px;");

    ui->pushButton_Get->setStyleSheet(
                "QPushButton{"
                "color: rgb(0, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(0, 0, 0);"
                "border-radius:10px;"
                "background-color: rgba(20,196,188, 210);"
                "font:28px '宋体';}"
  "QPushButton::hover{"
                "border:2px groove gray;"
                "border-color: rgb(255, 255, 255);"
                "background-color: rgba(251,102,102, 200);"
                "color:rgb(255,255,255);}"
   "QPushButton::pressed{"
                "color: rgb(255, 0, 0);"
                "border:2px groove gray;"
                "border-color: rgb(255, 0, 0);"
                "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(20,196,188, 210), stop:1 rgba(251,102,102, 200));}");



}

Reader::~Reader()
{
    db->close();
    delete ui;
}

void Reader::on_pushButton_Get_clicked()
{
    readeredit = new readerEdit(user);
    connect(readeredit,SIGNAL(signals_UserInfo(User_Info)),this,SLOT(update_User(User_Info)));
    readeredit->show();
}

void Reader::update_User(const User_Info &user_info)
{
    user = user_info;
    ui->LineEdit_Name->setText(user.realName);
    ui->lineEdit_Sex->setText(user.sex);
    ui->lineEdit_Email->setText(user.email);
    ui->lineEdit_TEL->setText(user.tel);
    ui->lineEdit_Address->setText(user.address);
    ui->lineEdit_Birthday->setText(user.birthday);


    ////在这里添加更新用户信息的语句，把user中所有的字段都更新进去
}

void Reader::getData()
{
    ////在这里添加连接数据库和获取数据库中用户信息和用户借书信息的select语句
    QSqlTableModel* model = new QSqlTableModel(this, DBOperation::getDatabase());
    model->setTable("reader_info");
    model->setFilter(QString("readerNumber='%0'").arg(QRoleReader::getInstance()->getReader()));
    if (model->select())
    {
        user.username = model->record(0).value("username").toString();
        user.password = model->record(0).value("password").toString();
        user.realName = model->record(0).value("realName").toString();
        user.sex =model->record(0).value("sex").toString();
        user.birthday = model->record(0).value("birthday").toString();
        user.address = model->record(0).value("address").toString();
        user.tel = model->record(0).value("tel").toString();
        user.email = model->record(0).value("email").toString();
        user.registerDate = model->record(0).value("registerDate").toDateTime().toString("yyyy-MM-dd hh:mm:ss");
        user.readerNumber = model->record(0).value("readerNumber").toString();
    }

    //ui->label_7->setStyleSheet("QLabel{border-image:url(:/user.png)}");

    ui->LineEdit_Name->setText(user.realName);
    ui->lineEdit_Sex->setText(user.sex);
    ui->lineEdit_Email->setText(user.email);
    ui->lineEdit_TEL->setText(user.tel);
    ui->lineEdit_Address->setText(user.address);
    ui->lineEdit_Birthday->setText(user.birthday);
    ui->readerNumber->setText("读者编号："+user.readerNumber);
    ui->reDate->setText("注册时间："+ user.registerDate);
}
